package com.cpa.admin.config;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhen.zhao01
 * @CreateDate 2018/11/14
 */
@Component
public class ErrorHandlerExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        Integer statusCode = response.getStatus();
        if(statusCode == null || statusCode == HttpStatus.NOT_FOUND.value()){
            return new ModelAndView("/404");
        } else if (statusCode == HttpStatus.UNAUTHORIZED.value()) {
            return new ModelAndView("/password_error");
        } else {
            ex.printStackTrace();
            return new ModelAndView("/error");
        }
    }
}
